package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.ElectronicPendulumdetailDataVo;
import com.ruoyi.tb.lmt.mapper.ElectronicPendulumdetailDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.ElectronicPendulumDataBo;
import com.ruoyi.tb.lmt.domain.vo.ElectronicPendulumDataVo;
import com.ruoyi.tb.lmt.domain.ElectronicPendulumData;
import com.ruoyi.tb.lmt.mapper.ElectronicPendulumDataMapper;
import com.ruoyi.tb.lmt.service.IElectronicPendulumDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 电子摆数据主表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-11
 */
@RequiredArgsConstructor
@Service
public class ElectronicPendulumDataServiceImpl implements IElectronicPendulumDataService {

    private final ElectronicPendulumDataMapper baseMapper;

    @Autowired
    private ElectronicPendulumDataMapper electronicPendulumDataMapper;

    @Autowired
    private ElectronicPendulumdetailDataMapper electronicPendulumdetailDataMapper;

    /**
     * 查询电子摆数据主表
     */
    @Override
    public ElectronicPendulumDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询电子摆数据主表列表
     */
    @Override
    public TableDataInfo<ElectronicPendulumDataVo> queryPageList(ElectronicPendulumDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ElectronicPendulumData> lqw = buildQueryWrapper(bo);
        Page<ElectronicPendulumDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电子摆数据主表列表
     */
    @Override
    public List<ElectronicPendulumDataVo> queryList(ElectronicPendulumDataBo bo) {
        LambdaQueryWrapper<ElectronicPendulumData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ElectronicPendulumData> buildQueryWrapper(ElectronicPendulumDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ElectronicPendulumData> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLmtId()), ElectronicPendulumData::getLmtId, bo.getLmtId());
        lqw.eq(StringUtils.isNotBlank(bo.getTestDate()), ElectronicPendulumData::getTestDate, bo.getTestDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), ElectronicPendulumData::getCompany, bo.getCompany());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), ElectronicPendulumData::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), ElectronicPendulumData::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getReportNumber()), ElectronicPendulumData::getReportNumber, bo.getReportNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProductModel()), ElectronicPendulumData::getProductModel, bo.getProductModel());
        lqw.eq(StringUtils.isNotBlank(bo.getProductFeature()), ElectronicPendulumData::getProductFeature, bo.getProductFeature());
        lqw.eq(StringUtils.isNotBlank(bo.getFullRange()), ElectronicPendulumData::getFullRange, bo.getFullRange());
        lqw.eq(StringUtils.isNotBlank(bo.getTestItem()), ElectronicPendulumData::getTestItem, bo.getTestItem());
        lqw.eq(StringUtils.isNotBlank(bo.getTestAttribute()), ElectronicPendulumData::getTestAttribute, bo.getTestAttribute());
        lqw.eq(StringUtils.isNotBlank(bo.getResponseTime()), ElectronicPendulumData::getResponseTime, bo.getResponseTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAmbientTemperature()), ElectronicPendulumData::getAmbientTemperature, bo.getAmbientTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getStandardTemperature()), ElectronicPendulumData::getStandardTemperature, bo.getStandardTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getAmbientHumidity()), ElectronicPendulumData::getAmbientHumidity, bo.getAmbientHumidity());
        lqw.eq(StringUtils.isNotBlank(bo.getStandardHumidity()), ElectronicPendulumData::getStandardHumidity, bo.getStandardHumidity());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxLimitError()), ElectronicPendulumData::getMaxLimitError, bo.getMaxLimitError());
        lqw.eq(StringUtils.isNotBlank(bo.getLimitErrorStandardValue()), ElectronicPendulumData::getLimitErrorStandardValue, bo.getLimitErrorStandardValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxLinearity()), ElectronicPendulumData::getMaxLinearity, bo.getMaxLinearity());
        lqw.eq(StringUtils.isNotBlank(bo.getLinearityStandardValue()), ElectronicPendulumData::getLinearityStandardValue, bo.getLinearityStandardValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxGapError()), ElectronicPendulumData::getMaxGapError, bo.getMaxGapError());
        lqw.eq(StringUtils.isNotBlank(bo.getGapErrorStandardValue()), ElectronicPendulumData::getGapErrorStandardValue, bo.getGapErrorStandardValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxAccuracy()), ElectronicPendulumData::getMaxAccuracy, bo.getMaxAccuracy());
        lqw.eq(StringUtils.isNotBlank(bo.getAccuracyStandardValue()), ElectronicPendulumData::getAccuracyStandardValue, bo.getAccuracyStandardValue());
        lqw.eq(StringUtils.isNotBlank(bo.getTestConclusion()), ElectronicPendulumData::getTestConclusion, bo.getTestConclusion());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), ElectronicPendulumData::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, ElectronicPendulumData::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), ElectronicPendulumData::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, ElectronicPendulumData::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增电子摆数据主表
     */
    @Override
    public Boolean insertByBo(ElectronicPendulumDataBo bo) {
        ElectronicPendulumData add = BeanUtil.toBean(bo, ElectronicPendulumData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改电子摆数据主表
     */
    @Override
    public Boolean updateByBo(ElectronicPendulumDataBo bo) {
        ElectronicPendulumData update = BeanUtil.toBean(bo, ElectronicPendulumData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ElectronicPendulumData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电子摆数据主表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean queryExistsByLmtIdAndTestDate(String lmtId, String testDate) {
        int count = electronicPendulumDataMapper.queryExistsByLmtIdAndTestDate(lmtId, testDate);
        return count > 0;
    }

    @Override
        public List<ElectronicPendulumdetailDataVo> selectPendulumData(String lmtId, String testDate, String reportNumber) {
        return electronicPendulumdetailDataMapper.selectPendulumData(lmtId, testDate, reportNumber);
    }
}
